clear;
clc;

Lpath={'0677845-R1-067-32_a'...   %the entire DB paths
  '100_0109'...
  '100_0497'...
  '110016671724'...
  '112255696146'...
  '113334665744'...
  '114591144943'...
  '20060319_087'...
  'aaa'...
  'animal_5_bg_020803'...
  'b14pavel013'...
  'b19objects118'...
  'b1chesnuttame'...
  'b1snake001'...
  'b20nature_landscapes129'...
  'b2chopper008'...
  'b2pods001'...
  'b4nature_animals_land009'...
  'b7nature_trees002'...
  'BBMF_Lancaster_july_06'...
  'beltaine_4_bg_050502'...
  'boy_float_lake'...
  'Bream_In_Basin'...
  'broom07'...
  'buggy_005'...
  'BW4'...
  'carriage'...
  'Carrigafoyle_Castle_Ext'...
  'caterpiller'...
  'chain98'...
  'chaom38'...
  'Cheeky_Penguin'...
  'CRW_0384'...
  'culzeancastle'...
  'DSC01236'...
  'DSC01239_d'...
  'DSC04575'...
  'DSCF0034_l'...
  'DSCF0459'...
  'DSCF3583'...
  'DSCF3623'...
  'DSCF3772'...
  'DSCN0756'...
  'DSCN1908'...
  'DSCN2064'...
  'DSCN2154'...
  'DSCN6805'...
  'DSC_0769'...
  'DSC_0959'...
  'DSC_422910022'...
  'egret_face'...
  'ESTB_IMG_6461_'...
  'europe_holiday_484'...
  'FullIceWater'...
  'hot_air_balloons_05'...
  'HPIM1242'...
  'HPIM5083_morguefile'...
  'Imagen_072__1_'...
  'IMGP2712'...
  'IMG_1516'...
  'IMG_1965'...
  'IMG_2528'...
  'IMG_2577'...
  'IMG_2592_f'...
  'Img_3083_modif'...
  'img_3803'...
  'IMG_4214'...
  'Img_4730_modif'...
  'IMG_7359_copia'...
  'ireland_62_bg_061502'...         %70
  'kconnors030466'...
  'leafpav'...
  'London_Zoo3'...
  'matsumt060102_DYJ08'...
  'mexico3'...
  'Moth061304_0679'...
  'nitpix_P1280114'...
  'osaka060102_DYJSN071'...
  'oscar2005_05_07'...
  'outside_guggenheim_walls'...         %80
  'palovigna'...
  'PIC0203VN0092'...
  'PIC0203VN0105'...
  'PIC106470172014'...
  'PIC1080629574'...
  'PIC109250805856'...
  'PIC1092515922117'...
  'PICT2272'...
  'PICT2605'...
  'Picture_458'...              %90
  'postjp'...
  'REDBERRY_RB03'...
  'San_Andres_130'...
  'SG_01_IMG_1943_tratada'...
  'Sharp_image'...
  'Skookumchuk_starfish1'...
  'snow2_004'...
  'tendrils'...
  'windowCN_0078'...
  'yokohm060409_DYJSN191'};


DBpath = 'D:\ImageSource\Weizmann_Seg_DB_1obj\';
for j=1:length(Lpath)
    filepath = strcat(DBpath,Lpath(j),'\src_color\',Lpath(j),'.png');
    I = imread(filepath{1});
    I = rgb2gray(I);
    m=size(I,1);
    n=size(I,2);
    gray=reshape(I,m*n,1);
    options = [2;1000;1e-5;1];
    claNum = 2;
    [uk,Ck] = kmeans(double(gray),claNum,'maxiter',2000,'Display','final','Replicates',1);
    [centerf,uf,objf] = fcm(double(gray),claNum,options);
    [centerkf,ukf,objkf] = kfcmFun(double(gray),claNum,1000,300,2);
    [~,indk] = sort(Ck);
    [~,indf] = sort(centerf);
    [~,indkf] = sort(centerkf);
    for i = 1 : size(uk,1)
        uk(i) = indk(uk(i));
    end
    uf = uf(indf,:);
    ukf = ukf(indkf,:);
    step = fix(256./claNum);
    [~,indexf] = max(uf);
    [~,indexkf] = max(ukf);
    grayk = zeros(size(gray));
    grayf = zeros(size(gray));
    graykf = zeros(size(gray));
    for i = 1 : claNum
        grayk(uk==i,:) = i*step;
        grayf(indexf==i,:) = i*step;
        graykf(indexkf==i,:) = i*step;
    end
    imgk = reshape(grayk,m,n);
    imgf = reshape(grayf,m,n);
    imgkf = reshape(graykf,m,n);
    kpath = strcat(DBpath,Lpath(j),'\kmeans');
    fpath = strcat(DBpath,Lpath(j),'\fcm');
    kfpath = strcat(DBpath,Lpath(j),'\kfcm');
%     cd(bpath{1});
    if ~exist(kpath{1})
        mkdir(kpath{1});
    end
    if ~exist(fpath{1})
        mkdir(fpath{1});
    end
    if ~exist(kfpath{1})
        mkdir(kfpath{1});
    end
    kmeanspath = strcat(kpath{1},'\',Lpath(j),'.png');
    fcmpath = strcat(fpath{1},'\',Lpath(j),'.png');
    kfcmpath = strcat(kfpath{1},'\',Lpath(j),'.png');
    imwrite(uint8(imgk),kmeanspath{1});
    imwrite(uint8(imgf),fcmpath{1});
    imwrite(uint8(imgkf),kfcmpath{1});
end;